//    Copyright (C) Kherty.  All rights reserved.
using System;

namespace OpenLS.Spreadsheet.StandardAddIn.Statistics
{
    internal class PoissonDistribution : IntegerDistribution
    {
        private double _mean;

        public PoissonDistribution(double mean)
        {
            Mean = mean;
        }

        public double Mean
        {
            get { return _mean; }
            set
            {
                if (value < 0)
                {
                    throw new ArgumentOutOfRangeException("value");
                }
                _mean = value;
            }
        }

        public override double Probability(int x)
        {
            if (x < 0 || x == int.MaxValue)
            {
                return 0;
            }
            return Math.Pow(Mean, x)/Statistician.FactorialDouble(x)*Math.Exp(-Mean);
        }

        public override double CumulativeProbability(int x)
        {
            if (x < 0)
                return 0;
            if (x == int.MaxValue)
                return 1;
            return Statistician.regularizedGammaQ((double) x + 1, _mean,
                                                  1E-12, int.MaxValue);
        }

        protected override int DomainLowerBound(double p)
        {
            return 0;
        }

        protected override int DomainUpperBound(double p)
        {
            return int.MaxValue;
        }
    }
}